<?php
/**
 * Created by PhpStorm.
 * User: Yangxufeng
 * Date: 2018/8/28
 * Time: 17:16
 */

namespace app\index\server;

use app\index\model\Message as MessageModel;
use app\lib\exception\DatabaseException;
use think\Exception;

class MessageServer extends BaseServer
{
    public function getList($params = [])
    {
        $prep_params = $this->prep_param($params);
        try {
            // 总条数
            $count = MessageModel::count();
            $list = MessageModel::with(['userId', 'adminId'])
                ->order($prep_params['sort'], $prep_params['order'])
                ->limit($prep_params['offset'], $prep_params['limit'])
                ->select();
        } catch (Exception $e) {
            // 有些问题 应该抛出错误异常
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return bootstrap_table_return($count, $list);
    }

    public function addOrUpdate($params = [])
    {

    }

    public function detail($id)
    {
        try{
            $detail = MessageModel::with(['userId', 'adminId'])
                ->where('id', '=', $id)
                ->find();
            if(!$detail){
                return setMsg(0, '没有查找到message数据');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                $e->getMessage()
            ]);
        }
        return setMsg(1, '查找到message数据', $detail);
    }

    public function changeStatus($id)
    {
        try{
            $info = MessageModel::where('id', '=', $id)->find();
            $info->status = abs( ($info->status) - 1 );
            $result = $info->save();
            if(!$result){
                return setMsg(0, '修改Message状态失败');
            }
        }catch(Exception $e){
            throw new DatabaseException([
                'msg' => $e->getMessage()
            ]);
        }
        return setMsg(1, '修改Message状态成功');
    }
}
